package com.betty.cscams.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.betty.cscams.common.page.MybatisPageHelper;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.common.page.PageResult;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.dao.SysDeptMapper;
import com.betty.cscams.dao.SysUserMapper;
import com.betty.cscams.entity.SysDept;
import com.betty.cscams.entity.SysUser;
import com.betty.cscams.service.SysDeptService;
import com.betty.cscams.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptMapper sysDeptMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public int save(SysDept record) {
		record.setLastUpdateBy(SecurityUtils.getUsername());
		record.setLastUpdateTime(new Date());
		if(record.getId() == null || record.getId() == 0) {
			record.setCreateBy(SecurityUtils.getUsername());
			record.setCreateTime(new Date());
			return sysDeptMapper.insertSelective(record);
		}
		return sysDeptMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int delete(SysDept record) {
		return sysDeptMapper.deleteByPrimaryKey(record.getId());
	}

	@Override
	public int delete(List<SysDept> records) {
		for(SysDept record:records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public SysDept findById(Long id) {
		return sysDeptMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest) {
		return MybatisPageHelper.findPage(pageRequest, sysDeptMapper);
	}

	@Override
	public List<SysDept> findTree() {
		List<SysDept> sysDepts = new ArrayList<>();
		List<SysDept> depts = sysDeptMapper.findAll();
		for (SysDept dept : depts) {
			if (dept.getParentId() == null || dept.getParentId() == 0) {
				dept.setLevel(0);
				dept.setParentName("重庆理工大学");
				sysDepts.add(dept);
			}
		}
		findChildren(sysDepts, depts);
		return sysDepts;
	}

	@Override
	public List<SysDept> findStudentTree() {
		List<SysDept> sysDepts = new ArrayList<>();
		List<SysDept> depts = sysDeptMapper.findAll();
		for (SysDept dept : depts) {
			if (dept.getParentId() == null || dept.getParentId() == 0) {
				dept.setLevel(0);
				dept.setParentName("重庆理工大学");
				sysDepts.add(dept);
			}
		}
		findStudentChildren(sysDepts, depts);
		return sysDepts;
	}

	private void findChildren(List<SysDept> sysDepts, List<SysDept> depts) {
		for (SysDept sysDept : sysDepts) {
			List<SysDept> children = new ArrayList<>();
			for (SysDept dept : depts) {
				if (sysDept.getId() != null && sysDept.getId().equals(dept.getParentId())) {
					dept.setParentName(sysDept.getName());
					dept.setLevel(sysDept.getLevel() + 1);
					children.add(dept);
				}
			}
			sysDept.setChildren(children);
			findChildren(children, depts);
		}
	}

	private void findStudentChildren(List<SysDept> sysDepts, List<SysDept> depts) {
		for (SysDept sysDept : sysDepts) {
			List<SysDept> children = new ArrayList<>();
			for (SysDept dept : depts) {
				if (sysDept.getId() != null && sysDept.getId().equals(dept.getParentId())) {
					dept.setParentName(sysDept.getName());
					dept.setLevel(sysDept.getLevel() + 1);
					children.add(dept);
				}
				//班级则添加学生
				if(dept.getType() == 3){
					List<SysUser> students = sysUserMapper.findListByDeptId(dept.getId());
					List<SysDept> deptStudents = new ArrayList<>();
					for (SysUser user:students) {
						SysDept sysDeptStudent = new SysDept();
						sysDeptStudent.setId(user.getId()*(-1));
						sysDeptStudent.setParentName(sysDept.getName());
						sysDeptStudent.setName(user.getNickName());
						sysDeptStudent.setType(8);
						deptStudents.add(sysDeptStudent);
					}
					dept.setChildren(deptStudents);
				}
			}
			sysDept.setChildren(children);
			findStudentChildren(children, depts);
		}
	}

}
